const { ethers } = require("hardhat");
require("dotenv").config();

async function main() {
  console.log("Starting deployment...");

  const [deployer] = await ethers.getSigners();
  console.log("Deploying contracts with the account:", deployer.address);

  const balance = await deployer.getBalance();
  console.log("Account balance:", ethers.utils.formatEther(balance), "BNB");

  // Deploy God Token
  console.log("\n1. Deploying God Token...");
  const GodToken = await ethers.getContractFactory("GodToken");
  const initialSupply = 100000000; // 100 million tokens initially
  const godToken = await GodToken.deploy(
    "God Token",
    "GOD",
    deployer.address,
    initialSupply
  );

  await godToken.deployed();
  console.log("God Token deployed to:", godToken.address);
  console.log("Total supply:", ethers.utils.formatEther(await godToken.totalSupply()), "GOD");

  // Deploy God Staking
  console.log("\n2. Deploying God Staking...");
  const GodStaking = await ethers.getContractFactory("GodStaking");
  const godStaking = await GodStaking.deploy(godToken.address, deployer.address);

  await godStaking.deployed();
  console.log("God Staking deployed to:", godStaking.address);

  // Set staking contract in token
  console.log("\n3. Setting staking contract in God Token...");
  await godToken.setStakingContract(godStaking.address);
  console.log("Staking contract set successfully");

  // Transfer some tokens to staking contract for rewards
  console.log("\n4. Funding staking contract with rewards...");
  const rewardAmount = ethers.utils.parseEther("10000000"); // 10 million tokens for rewards
  await godToken.transfer(godStaking.address, rewardAmount);
  console.log("Transferred", ethers.utils.formatEther(rewardAmount), "GOD to staking contract");

  // Set initial settings
  console.log("\n5. Setting initial staking parameters...");
  await godStaking.setDistributionSettings(true, 1, 14 * 60 * 60); // 1 hour deadline, 2 PM UTC
  console.log("Staking parameters configured");

  // Deployment summary
  console.log("\n=== Deployment Summary ===");
  console.log("God Token:", godToken.address);
  console.log("God Staking:", godStaking.address);
  console.log("Deployer:", deployer.address);
  console.log("Network:", network.name);
  console.log("Gas used:", (await deployer.getBalance()).sub(balance).toString());

  // Save deployment info
  const deploymentInfo = {
    network: network.name,
    deployer: deployer.address,
    contracts: {
      godToken: godToken.address,
      godStaking: godStaking.address
    },
    timestamp: new Date().toISOString(),
    gasUsed: (await deployer.getBalance()).sub(balance).toString()
  };

  const fs = require("fs");
  fs.writeFileSync(
    `deployment-${network.name}-${Date.now()}.json`,
    JSON.stringify(deploymentInfo, null, 2)
  );

  console.log("\nDeployment info saved to deployment file");

  if (network.name !== "hardhat") {
    console.log("\n=== Verification Commands ===");
    console.log("Verify God Token:");
    console.log(`npx hardhat verify --network ${network.name} ${godToken.address} "God Token" "GOD" "${deployer.address}" ${initialSupply}`);

    console.log("\nVerify God Staking:");
    console.log(`npx hardhat verify --network ${network.name} ${godStaking.address} ${godToken.address} ${deployer.address}`);
  }
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });